package vn.edu.saigontech.ncms.web.bookstore;

import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.service.ItemService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateOrderDetailAction extends AbstractValidator {
	private Long itemId;
	private String quantity;
	
	@Override
	public String check() {
		if(Checker.isNull(this.itemId)) {
			this.sendKeyError("validator.required", "bookstore.item");
		}
		if(Checker.isEmpty(this.quantity)) {
			this.sendKeyError("validator.required", "quantity");
		}
		if(!Checker.isInteger(this.quantity)) {
			this.sendKeyError("validator.invalid", "quantity");
		}
		Item item = ItemService.INSTANCE.findById(this.itemId);
		if(item != null) {
			Long require = Long.parseLong(this.quantity);
			if(item.getQuantity() < require) {
				this.sendKeyError("validator.notEnough", item.getDisplayName(), item.getQuantity() + "", "Volume");
			}
		}
		return this.toSuccess();
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}
}
